library classs;

import 'dart:math';

class Point {
  num x;
  num y;
  num z = 0;

  Point(this.x,this.y);

  void show() {
    print('x $x y $y');
  }

  num distanceTo(Point other) {
    var dx = x - other.x;
    var dy = y - other.y;
    return sqrt(dx* dx + dy*dy);
  }

  // 静态方法(类方法)不对实例进行操作，因此无法访问该实例
  // 对于通用或广泛使用的实用程序和功能，考虑使用顶级函数，而不是静态方法
  // 可以使用静态方法作为编译时常量。例如，可以将静态方法作为参数传递给常量构造函数
  /**
void main() {
  var a = Point(2, 2);
  var b = Point(4, 4);
  var distance = Point.distanceBetween(a, b);
  assert(2.8 < distance && distance < 2.9);
  print(distance);
}
   */
  static num distanceBetween(Point a, Point b) {
    var dx = a.x - b.x;
    var dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
  }
}

class Person {
  String firstname;

  Person.fromJson(Map data) {
    print('in Person');
  }
}

// 继承
class Employee extends Person {
  Employee.fromJson(Map data): super.fromJson(data) {
    print('in Employee');
  }
}

class Animal {
  num x;
  num y;
  Animal.toJson(Map<String,num> json)
      : x = json['x'], 
        y = json['y'] {
    print('In Animal.fromJson(): ($x,$y) ');
  }
}

class Rectangle {
  num left ,top , width,height = 0;

  Rectangle(this.left,this.top,this.width,this.height);

  num get right => left + width;
  set right(num value) => left = value - width;
  num get bottom => top + height;
  set bottom(num value) => top = value - height;
}

// A person. The implicit interface contains greet().
class Person1 {
  // In the interface, but visible only in this library.
  final _name;

  // Not in the interface, since this is a constructor.
  Person1(this._name);

  // In the interface.
  String greet(String who) => 'Hello, $who. I am $_name.';
}

// 实现
// An implementation of the Person interface.
class Impostor implements Person1 {
  get _name => '';

  String greet(String who) => 'Hi $who. Do you know who I am?';
}

String greetBob(Person1 person) => person.greet('Bob');

// 扩展一个类（继承）
class Television {
  void turnOn() {
    _illuminateDisplay();
    _activateIrSensor();
  }

  var _illuminateDisplay = () => print('ok');
  var _activateIrSensor = () => print('ok');
}

class SmartTv extends Television {
  @override
  void turnOn() {
    super.turnOn();
    _bootNetworkInterface();
    _initializeMemory();
    _upgradeApps();
  }
  var _bootNetworkInterface = () => print('ok');
  var _initializeMemory = () => print('ok');
  var _upgradeApps = () => print('ok');
}

// 要实现mixin，创建一个Object的子类，不声明构造函数，也不调用super。例如:
abstract class Musical {
  bool canPlayPiano = false;
  bool canCompose = false;
  bool canConduct = false;

  void entertainMe() {
    if (canPlayPiano) {
      print('Playing piano');
    } else if (canConduct) {
      print('Waving hands');
    } else {
      print('Humming to self');
    }
  }
}

// 为类添加mixins特性
class Musician extends SmartTv with Impostor {}

class Maestro extends SmartTv with Impostor,Musical {
  String name;
  Maestro(String name) {
    this.name = name;
  }
}